package com.pinyoumei.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.BaseBean;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.AppManager;
import com.pinyoumei.app.utils.EventBusUtils;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.StatusBarUtils;
import com.pinyoumei.app.utils.ToastUtils;
import com.pinyoumei.app.utils.event.BackBuyEvent;
import com.pinyoumei.app.utils.event.GroupEvent;
import com.pinyoumei.app.utils.event.RecharEvent;
import com.pinyoumei.app.utils.event.UserEvent;
import com.pinyoumei.app.view.LoadingDailog;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2021-08-26.
 * --------- 日期 ------- 维护人 ------------ 变更内容 --------
 * 2021-08-26        Administrator             新增Person类
 * 2021-08-26        Administrator             增加sex属性
 * 实名认证
 */
public class ReNameActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText mEtName;
    private EditText mEtIdcard;
    private EditText mEtZfb;
    private String kf_url = "";
    private AppManager appManager = AppManager.getInstance();
    private Gson gson;
    private LoadingDailog loadingDailog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //当FitsSystemWindows设置 true 时，会在屏幕最上方预留出状态栏高度的 padding
        StatusBarUtils.setRootViewFitsSystemWindows(this,true);
        //设置状态栏透明
        StatusBarUtils.setTranslucentStatus(this);
        //一般的手机的状态栏文字和图标都是白色的, 可如果你的应用也是纯白色的, 或导致状态栏文字看不清
        //所以如果你是这种情况,请使用以下代码, 设置状态使用深色文字图标风格, 否则你可以选择性注释掉这个if内容
        if (!StatusBarUtils.setStatusBarDarkTheme(this, true)){
            //如果不支持设置深色风格 为了兼容总不能让状态栏白白的看不清, 于是设置一个状态栏颜色为半透明,
            //这样半透明+白=灰, 状态栏的文字能看得清
            StatusBarUtils.setStatusBarColor(this,0x55000000);
        }
        // 添加activity 到栈
        appManager.addActivity(this);

        gson = new Gson();
        LoadingDailog.Builder builder = new LoadingDailog.Builder(this)
                .setMessage("加载中...")
                .setCancelable(true);
        loadingDailog = builder.create();

        setContentView(R.layout.activity_pt_rename);
        kf_url = getIntent().getStringExtra("kf_url");
        InitWidget();

    }

    private void InitWidget() {

        ImageView mIvClose = findViewById(R.id.iv_close);
        mIvClose.setOnClickListener(this);
        mEtName = findViewById(R.id.et_name);
        mEtIdcard = findViewById(R.id.et_idcard);
        mEtZfb = findViewById(R.id.et_zfb);
        Button  mBtSend = findViewById(R.id.bt_send);
        mBtSend.setOnClickListener(this);

        LinearLayout  mLlKf = findViewById(R.id.ll_kf);
        mLlKf.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        	case R.id.iv_close:
        		finish();
        		break;
        		case R.id.bt_send:
        		    String name = mEtName.getText().toString().trim();
        		    String Idcard = mEtIdcard.getText().toString().trim();
        		    String zfb = mEtZfb.getText().toString().trim();

        		    if (TextUtils.isEmpty(name)){
                        ToastUtils.showShort("请输入真实姓名");
                    }else if (TextUtils.isEmpty(Idcard)){
                        ToastUtils.showShort("请输入身份证号");
                    }else if (TextUtils.isEmpty(zfb)){
                        ToastUtils.showShort("请输入支付宝账号");
                    }else{
                        SendData(name,Idcard,zfb);
                    }

        		break;
        		case R.id.ll_kf:
                    //客服
                    if (!TextUtils.isEmpty(kf_url)){
                        final Intent intent2 = new Intent(ReNameActivity.this, WebViewActivity.class);

                        intent2.putExtra("order_title", "");
                        intent2.putExtra("url",kf_url);
                        startActivity(intent2);
                    }
        		break;

        	default:
        		break;
        }
    }

    private void SendData(String user_real_name,String user_id_no, String user_alipay_account) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", SPUtils.getAppUserId(ReNameActivity.this));
        map.put("user_real_name", user_real_name);
        map.put("user_id_no", user_id_no);
        map.put("user_alipay_account", user_alipay_account);


        OkGo.<String>post(Constants.REAL_NAME)
                .params("user_id", SPUtils.getAppUserId(ReNameActivity.this))
                .params("user_real_name", user_real_name)
                .params("user_id_no", user_id_no)
                .params("user_alipay_account", user_alipay_account)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {


                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onCacheSuccess(Response<String> response) {
                        super.onCacheSuccess(response);
                        setData(response);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        setData(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }

                    private void setData(Response<String> response) {
                        loadingDailog.dismiss();
                        BaseBean signBean = gson.fromJson(response.body(), BaseBean.class);

                        if (signBean.getCode() >= 0) {
                            EventBusUtils.post(new UserEvent("1"));
                            EventBusUtils.post(new RecharEvent("1"));
                            EventBusUtils.post(new GroupEvent("1"));
                            EventBusUtils.post(new BackBuyEvent("1"));
                            Toast.makeText(ReNameActivity.this, signBean.getMsg(), Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(ReNameActivity.this, signBean.getMsg(), Toast.LENGTH_SHORT).show();
                        }

                    }

                });
    }
}
